package com.fanqiechaodan.command;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fanqiechaodan
 * @Classname Invoker
 * @Description 命令请求者
 */
public class Invoker {

    private List<Command> commandList = new ArrayList<>();

    /**
     * 添加命令
     *
     * @param command
     */
    public void addCommand(Command command) {
        commandList.add(command);
    }

    /**
     * 执行命令
     */
    public void executeCommands() {
        commandList.forEach(Command::execute);
    }

    /**
     * 撤销命令
     */
    public void undoCommands() {
        for (int i = commandList.size() - 1; i >= 0; i--) {
            commandList.get(i).undo();
        }
    }
}
